package com.og.managedbean;

import java.io.Serializable;
import java.util.Date;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;

import com.og.backingbean.Module;
import com.og.backingbean.ModuleHistory;
import com.og.backingbean.Type;
import com.og.dao.CRUDEntityDAO;

/**
 * @author Renaud DIANA
 *
 */
@Named
@SessionScoped
public class ModuleBean implements Serializable {

	private static final long serialVersionUID = 1L;

	// Log4j	 
	private static final Logger	log	= Logger.getLogger(ModuleBean.class);

	/* Dependency injection */
	// Model
    @Inject
    private CRUDEntityDAO<Module> moduleDAO;
    @Inject
    private CRUDEntityDAO<ModuleHistory> moduleHistoryDAO;
    // BackingBean    
    @Inject
    private Type type;
    @Inject
    private Module moduleNew;
    @Inject
    private Module moduleView;
    @Inject
    private Module moduleUpdate; 
    @Inject
    private ModuleHistory moduleHistory;
    @Inject
    private ListBean list;
	        
	/* -------------------- */
	    
	/**
	 * Default constructor
	 */
	public ModuleBean() {
		// TODO Auto-generated constructor stub
	}

	public String create() {
		
		// Call model		
		moduleDAO.createEntity(moduleNew);
		
		// Generate new list
		list.findAllModule();
		
		// Clear form
		moduleNew = new Module();
						
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Create module Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}	
	
	public String update() {
		
		// Check version for history
		if(!moduleUpdate.getVersion().equals(moduleHistory.getVersion())) {
			
			// Set applicationHistory
			moduleHistory.setModule(moduleUpdate);
			moduleHistory.setDate(moduleUpdate.getDate());			
			moduleHistory.setPrice(moduleUpdate.getPrice());
			
			// Call model
			moduleHistoryDAO.createEntity(moduleHistory);
								
			// Reset form
			moduleHistory = new ModuleHistory();
			
			// Generate new list
			list.findAllModuleHistory();
						
			// Add message
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Create moduleHistory Successful"));
		}
		
		// Call model				
		moduleDAO.updateEntity(moduleUpdate);
		
		// Generate new list
		list.findAllModule();
		
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Update module Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String delete() {
				
		// Call model				
		moduleDAO.deleteEntity(moduleView);
		
		// Generate new list
		list.findAllModule();
		
		// Clear form
		moduleView = new Module();
				
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete module Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String findOne() {
		
		// Call model				
		moduleView = moduleDAO.findOneEntity(moduleView, moduleView.getIdModule());
		
		// Populate person(s)
		//person = company.getListPersons().get(0);
						
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Find module Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}

	public String toggleUpdate() {
		
		moduleUpdate = moduleView;
		moduleHistory.setVersion(moduleView.getVersion());
		
		return "";
	}
	
	// Getters and Setters
	
	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public Module getModuleNew() {
		return moduleNew;
	}

	public void setModuleNew(Module moduleNew) {
		this.moduleNew = moduleNew;
	}

	public Module getModuleView() {
		return moduleView;
	}

	public void setModuleView(Module moduleView) {
		this.moduleView = moduleView;
	}

	public Module getModuleUpdate() {
		return moduleUpdate;
	}

	public void setModuleUpdate(Module moduleUpdate) {
		this.moduleUpdate = moduleUpdate;
	}

	public ModuleHistory getModuleHistory() {
		return moduleHistory;
	}

	public void setModuleHistory(ModuleHistory moduleHistory) {
		this.moduleHistory = moduleHistory;
	}
}
